package com.xxq.semester.controller.ai;


import com.xxq.semester.entity.ai.AiResources;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.ai.IAiResourcesService;
import com.xxq.semester.service.impl.ai.AiResourcesServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequestMapping("/api/ai-resources")
@Tag(name="AI资源管理", description = "AI资源相关操作")
public class AiResourcesController {
    @Autowired
    private IAiResourcesService aiResourcesService;
@PostMapping("/upload")
@Operation(summary = "上传资源", description = "上传AI资源文件")
public ResponseEntity<AiResources> upload(@RequestBody UploadRequest request) {
    AiResources aiResource = new AiResources();
    aiResource.setOssUrl(request.getFileUrl());
    aiResource.setType(request.getFileType());
    aiResource.setChatId(request.getChatID());
    aiResource.setUserId(request.getUserID());
    aiResource.setCreatedAt(LocalDateTime.now());
    aiResource.setTitle(request.getTitle());

    aiResourcesService.save(aiResource);
    return ResponseEntity.success(aiResource);
}

    @GetMapping("/list")
    @Operation(summary = "获取AI资源列表", description = "获取指定用户的AI资源列表")
    public ResponseEntity<List<AiResources>> list(@RequestParam("userID") Integer userID) {
        // 调用服务层方法获取资源列表
        return ResponseEntity.success(aiResourcesService.listByUserId(userID));
    }
    @GetMapping("/{id}")
    @Operation(summary = "获取AI资源详情", description = "根据ID获取AI资源的详细信息")
    public ResponseEntity<AiResources> getResourceById(@PathVariable("id") Integer id) {
        AiResources aiResource = aiResourcesService.getById(id);
        if (aiResource == null) {
            return ResponseEntity.error("资源不存在");
        }
        return ResponseEntity.success(aiResource);
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除AI资源", description = "根据ID删除指定的AI资源")
    public ResponseEntity<String> deleteResource(@PathVariable("id") Integer id) {
        AiResources aiResource = aiResourcesService.getById(id);
        if (aiResource == null) {
            return ResponseEntity.error("资源不存在");
        }
        // 删除数据库记录
        aiResourcesService.removeById(id);
        return ResponseEntity.success("资源删除成功");
    }

}
// UploadRequest.java
class UploadRequest {
    private String fileUrl;
    private String fileType;
    private Integer chatID;
    private Integer userID;
    private String title;

    // ✅ 记得加上 getter/setter 或使用 Lombok @Data
    public String getFileUrl() { return fileUrl; }
    public void setFileUrl(String fileUrl) { this.fileUrl = fileUrl; }

    public String getFileType() { return fileType; }
    public void setFileType(String fileType) { this.fileType = fileType; }

    public Integer getChatID() { return chatID; }
    public void setChatID(Integer chatID) { this.chatID = chatID; }

    public Integer getUserID() { return userID; }
    public void setUserID(Integer userID) { this.userID = userID; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
}

